package socket;

import java.net.ServerSocket;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

class SSLBuilder implements SocketBuilder {
	
	private String[] ciphers;
	
	public SSLBuilder(String[] ciphers){
		
		if( ciphers != null){
			
			this.ciphers = ciphers;
		}
		else new NullPointerException("SSL ciphers cannot be null");
	}
	

	public Socket buildSocket(String host, int port) throws Exception{
		
		SocketFactory sf = SSLSocketFactory.getDefault();
	    
		Socket socket = sf.createSocket(host, port);
		
		((SSLSocket) socket).setEnabledCipherSuites(ciphers);
		
		return socket;
	}
	

	public ServerSocket buildServer(int port) throws Exception {
		
		SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
	    
		ServerSocket server = ssf.createServerSocket(port);
		
		((SSLServerSocket) server).setEnabledCipherSuites(ciphers);
		
		return server;
	}
}
